﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ChinaClipping.ClippingMaster.Controls
{
	public partial class ClippingProperty : UserControl
	{
		private Control _valuecontrol;
		private string _propertyname;
		private PropertyType _type;
		private IDictionary<string, string> _cbxdatasource;

		private bool _multiSelect;


		public string PropertyText
		{
			get { return label_PropertyText.Text; }
		}

		public string PropertyName
		{
			get { return _propertyname; }
			set { _propertyname = value; }
		}

		public string PropertyValue
		{
			get
			{
				if (_multiSelect == false)
				{
					ComboBox cbx = _valuecontrol as ComboBox;
					if (cbx != null)
					{
						if (_cbxdatasource.ContainsKey(cbx.Text))
						{
							return _cbxdatasource[cbx.Text];
						}
						else
						{
							return "";
						}
					}

					else
						return _valuecontrol.Text.Trim();
				}
				else
				{
					FlowLayoutPanel panel = _valuecontrol as FlowLayoutPanel;
					if (panel != null)
					{
						List<string> selectedIds = new List<string>();
						foreach (CheckBox cbx in panel.Controls)
						{
							if (cbx.Checked)
							{
								selectedIds.Add(cbx.Tag as string);
							}
						}
						return string.Join(",", selectedIds.ToArray());
					}
					else
					{
						return string.Empty;
					}

				}
			}
			set { _valuecontrol.Text = value; }
		}

		public PropertyType Type
		{
			get
			{
				return this._type;
			}
		}
		public IDictionary<string, string> Options
		{
			get;
			private set;
		}

		protected override void OnLoad(EventArgs e)
		{
			if (label_PropertyText.Width < 50)
				label_PropertyText.Width = 50;
			base.OnLoad(e);
		}

		/// <summary>
		/// 构造Filed类型的
		/// </summary>
		/// <param name="text"></param>
		/// <param name="name"></param>
		public ClippingProperty(string text, string name)
		{

			InitializeComponent();
			label_PropertyText.Text = text;
			_propertyname = name;
			_valuecontrol = new TextBox();
			_valuecontrol.Dock = DockStyle.Fill;
			this._type = PropertyType.Field;
			this.tableLayoutPanel1.Controls.Add(_valuecontrol, 1, 0);
		}


		/// <summary>
		/// 构造Property类型的
		/// </summary>
		/// <param name="text"></param>
		/// <param name="name"></param>
		/// <param name="dataSource"></param>
		public ClippingProperty(string text, string name, IDictionary<string, string> dataSource, bool multiSelect)
		{

			InitializeComponent();
			label_PropertyText.Text = text;
			_propertyname = name;
			_multiSelect = multiSelect;
			this.Options = dataSource;
			if (multiSelect == false)
			{
				ComboBox cbx = new ComboBox();
				cbx.DropDownStyle = ComboBoxStyle.DropDownList;
				this._cbxdatasource = dataSource;
				foreach (string k in dataSource.Keys)
				{
					cbx.Items.Add(k);
				}

				Graphics g = cbx.CreateGraphics();

				int width = cbx.DropDownWidth;
				int scrollBarWidth = 0;
				if (cbx.Items.Count > cbx.MaxDropDownItems)
					scrollBarWidth = SystemInformation.VerticalScrollBarWidth;
				foreach (object s in cbx.Items)
				{
					if (s != null)
					{

						int newWidth = (int)g.MeasureString(s.ToString().Trim(), cbx.Font).Width + scrollBarWidth;
						if (newWidth > width)
							width = newWidth;
					}

				}
				cbx.DropDownWidth = width;

				cbx.Dock = DockStyle.Fill;
				_valuecontrol = cbx;

			}
			else
			{
				FlowLayoutPanel panel = new FlowLayoutPanel();

				panel.AutoSize = true;
				panel.Dock = DockStyle.Fill;
				panel.AutoScroll = true;
				panel.VerticalScroll.Enabled = true;
				panel.HorizontalScroll.Enabled = false;
				//panel.ClientSizeChanged += new EventHandler(panel_ClientSizeChanged);
				//panel.SizeChanged += new EventHandler(panel_SizeChanged);

				foreach (string key in dataSource.Keys)
				{
					CheckBox cbx = new CheckBox();

					cbx.Text = key;
					cbx.Tag = dataSource[key];
					cbx.AutoSize = true;
					panel.Controls.Add(cbx);
				}
				this._valuecontrol = panel;
			}

			this._type = PropertyType.Property;
			this.tableLayoutPanel1.Controls.Add(_valuecontrol, 1, 0);
		}

		//void panel_SizeChanged(object sender, EventArgs e)
		//{
		//    int controlheight = this.Height;
		//    int controlwidth = this.Width;

		//    int currentRowWidth = 0;
		//    int currentRowHeight = 0;
		//    int totalHeight = 0;

		//    foreach (Control c in this._valuecontrol.Controls)
		//    {
		//        var cHeight = c.Height;
		//        var cWidth = c.Width;
		//        if (currentRowWidth + cWidth < controlwidth)
		//        {
		//            currentRowWidth += cWidth;
		//            currentRowHeight = Math.Max(currentRowHeight, cHeight);
		//        }
		//        else if (currentRowWidth == 0)
		//        {
		//            totalHeight += currentRowWidth+4;
		//            currentRowHeight = cHeight;
		//        }
		//        else
		//        {
		//            totalHeight += currentRowHeight+4;
		//            currentRowWidth = cWidth;
		//            currentRowHeight = cHeight;
		//        }
		//    }
		//    totalHeight += currentRowHeight+4;
		//    FlowLayoutPanel panel = this._valuecontrol as FlowLayoutPanel;

		//    //if (totalHeight > 56)
		//    //{
		//    //    panel.Height = 56;
		//    //    panel.AutoScroll = true;
		//    //}
		//    //else
		//    //{
		//        this.Height = totalHeight;
		//    //    panel.AutoScroll = false;
		//    //}
		//}

		//void panel_ClientSizeChanged(object sender, EventArgs e)
		//{

		//    //throw new NotImplementedException();
		//}


		/// <summary>
		/// 构造Link类型的
		/// </summary>
		/// <param name="text"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public static ClippingProperty GetLinkClippingProperty(string text, string name)
		{
			ClippingProperty rtn = new ClippingProperty(text, name);
			TextBox tbx = rtn._valuecontrol as TextBox;
			tbx.Multiline = true;
			tbx.ScrollBars = ScrollBars.Vertical;
			rtn._type = PropertyType.Link;
			return rtn;

		}


		public string[] Links
		{
			get
			{
				TextBox tbx = this._valuecontrol as TextBox;
				if (this._type == PropertyType.Link && tbx != null)
				{
					return tbx.Lines;
				}
				else
					return null;
			}
		}

		public bool Multiline
		{
			get
			{
				TextBox tb = _valuecontrol as TextBox;
				if (tb == null)
					return false;
				else
				{
					return tb.Multiline;
				}
			}
			set
			{
				TextBox tb = _valuecontrol as TextBox;
				if (tb != null)
				{
					tb.Multiline = value;
				}
			}
		}

		public int getDesiredHeight()
		{
			int currentWidth = this.Width;
			int rowCount = 1;
			int currentRowWidth = 0;
	
			foreach (Control c in this._valuecontrol.Controls)
			{
				if (currentRowWidth + c.Width +4< currentWidth)
				{
					currentRowWidth = currentRowWidth + c.Width+4;

				}
				else
				{
					if (c.Width +4> currentWidth)
					{
						currentRowWidth = 0;
						rowCount += 1;
					}
					else
					{
						currentRowWidth = c.Width+4;

					}
					rowCount += 1;

				}
			}

			return 28 * rowCount;
		}
	}

	public enum PropertyType
	{
		Field,
		Property,
		Link
	}
}
